package com.itheima.reggie.controller;


import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
public class OrdersController {


    @Autowired(required = false)
    private OrdersService ordersService;
    /**
     * 作用： 下单
     * @param orders
     * @param session
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpSession session){
        //1.得到登陆者
        Long userId = (Long) session.getAttribute("user");
        //下单
        ordersService.addOrders(orders,userId);
        return R.success("下单成功");
    }

    /**
     * 查看订单；移动端
     * @param page
     * @param pageSize
     * @param session
     * @return
     */
    @GetMapping("userPage")
    public R< Page<OrdersDto>> userPage(Integer page, Integer pageSize, HttpSession session, String name, LocalDateTime startTime, LocalDateTime endTime){
        Long userId = (Long) session.getAttribute("user");
        Page<OrdersDto> ordersDtoPage = ordersService.findOrdersByUserId(page, pageSize, userId,name,startTime,endTime);
        return R.success(ordersDtoPage);
    }


    /**
     * 查看订单：后台管理
     * @param page
     * @param pageSize
     * @param session
     * @return
     */
    @GetMapping("page")
    public R< Page<OrdersDto>> page(Integer page,Integer pageSize, String number,HttpSession session, LocalDateTime startTime, LocalDateTime endTime){
        Long userId = (Long) session.getAttribute("user");
        Page<OrdersDto> ordersDtoPage = ordersService.findOrdersByUserId(page, pageSize, userId,number,startTime,endTime);
        return R.success(ordersDtoPage);
    }
}
